#include "ImgBasicProcessing.h"

ImgBasicProcessing::ImgBasicProcessing(QImage *m_Img1)
{
    m_Img = m_Img1;
}

QImage* ImgBasicProcessing::toGray()
{
    QImage *newImg = new QImage(m_Img->width(), m_Img->height(), QImage::Format_ARGB32);
    for(int y = 0; y<m_Img->height(); y++)
    {
        QRgb * line = (QRgb *)m_Img->scanLine(y);
        for(int x = 0; x<m_Img->width(); x++)
        {
            int average = (qRed(line[x]) + qGreen(line[x]) + qBlue(line[x]))/3;
            newImg->setPixel(x,y, qRgb(average, average, average));
        }
    }
    return newImg;
}
QImage* ImgBasicProcessing::toChangeBrightness(int delta)
{
    QImage *newImg = new QImage(m_Img->width(), m_Img->height(), QImage::Format_ARGB32);
    for(int y = 0; y<m_Img->height(); y++)
    {
        QRgb * line = (QRgb *)m_Img->scanLine(y);
        for(int x = 0; x<m_Img->width(); x++)
        {
            int r = qRed(line[x])+delta;
            int g = qGreen(line[x])+delta;
            int b = qBlue(line[x])+delta;
            if(r > 255)  r = 255;
            if(r < 0)  r = 0;
            if(g > 255)  g = 255;
            if(g < 0)  g = 0;
            if(b > 255)  b = 255;
            if(b < 0)  b = 0;
            newImg->setPixel(x,y, qRgb(r,g,b));
        }
    }
    return newImg;
}
QImage* ImgBasicProcessing::toWarmColor(int delta)
{
    QImage *newImg = new QImage(m_Img->width(), m_Img->height(), QImage::Format_ARGB32);
    for(int y = 0; y<m_Img->height(); y++)
    {
        QRgb * line = (QRgb *)m_Img->scanLine(y);
        for(int x = 0; x<m_Img->width(); x++)
        {
            int r = qRed(line[x])+delta;
            int g = qGreen(line[x])+delta;
            int b = qBlue(line[x]);
            if(r > 255)  r = 255;
            if(r < 0)  r = 0;
            if(g > 255)  g = 255;
            if(g < 0)  g = 0;
            newImg->setPixel(x,y, qRgb(r,g,b));
        }
    }
    return newImg;

}
QImage* ImgBasicProcessing::toColdColor(int delta)
{
    QImage *newImg = new QImage(m_Img->width(), m_Img->height(), QImage::Format_ARGB32);
    for(int y = 0; y<m_Img->height(); y++)
    {
        QRgb * line = (QRgb *)m_Img->scanLine(y);
        for(int x = 0; x<m_Img->width(); x++)
        {
            int r = qRed(line[x]);
            int g = qGreen(line[x]);
            int b = qBlue(line[x])+delta;
            if(b > 255)  b = 255;
            if(b < 0)  b = 0;
            newImg->setPixel(x,y, qRgb(r,g,b));
        }
    }
    return newImg;
}
QImage* ImgBasicProcessing::toChangeSaturation(int delta)
{
    QImage * newImage = new QImage(m_Img->width(), m_Img->height(), QImage::Format_ARGB32);
    QColor oldColor;
    QColor newColor;
    int h,s,l;
    for(int x=0; x<newImage->width(); x++){
        for(int y=0; y<newImage->height(); y++){
            oldColor = QColor(m_Img->pixel(x,y));
            newColor = oldColor.toHsl();
            h = newColor.hue();
            s = newColor.saturation()+delta;
            l = newColor.lightness();
            //we check if the new value is between 0 and 255
            s = qBound(0, s, 255);
            newColor.setHsl(h, s, l);
            newImage->setPixel(x, y, qRgb(newColor.red(), newColor.green(), newColor.blue()));
        }
    }
    return newImage;
}
QImage* ImgBasicProcessing::toSharpen()
{
    QImage * newImage = new QImage(* m_Img);

    int kernel [3][3]= {{0,-1,0},
                        {-1,5,-1},
                        {0,-1,0}};
    int kernelSize = 3;
    int sumKernel = 1;
    int r,g,b;
    QColor color;

    for(int x=kernelSize/2; x<newImage->width()-(kernelSize/2); x++){
        for(int y=kernelSize/2; y<newImage->height()-(kernelSize/2); y++){

            r = 0;
            g = 0;
            b = 0;

            for(int i = -kernelSize/2; i<= kernelSize/2; i++){
                for(int j = -kernelSize/2; j<= kernelSize/2; j++){
                    color = QColor(m_Img->pixel(x+i, y+j));
                    r += color.red()*kernel[kernelSize/2+i][kernelSize/2+j];
                    g += color.green()*kernel[kernelSize/2+i][kernelSize/2+j];
                    b += color.blue()*kernel[kernelSize/2+i][kernelSize/2+j];
                }
            }

            r = qBound(0, r/sumKernel, 255);
            g = qBound(0, g/sumKernel, 255);
            b = qBound(0, b/sumKernel, 255);

            newImage->setPixel(x,y, qRgb(r,g,b));

        }
    }
    return newImage;
}


